var mode ;
    var globalId = 0; //1 = quick edit, 0 = reply
    var oFCKeditor = 0;

    function FCKeditor_OnComplete( editorInstance )
    {
        oEditor = FCKeditorAPI.GetInstance('comment_text');
        if(mode == 1)
        {                                                                                                
            txt = $("#comment-text-" + globalId).text();
            oEditor = FCKeditorAPI.GetInstance('comment_text');
            oEditor.SetHTML(txt);
        }
        else
        	 oEditor.SetHTML('');
        	
    }
    function create_Fck()
    {
         var div = document.getElementById("comment_text_wrapper");
         oFCKeditor = new FCKeditor("comment_text");
         oFCKeditor.BasePath = "/public/library/fckeditor/";
         oFCKeditor.Height = 200;
         oFCKeditor.Width = 500;
         oFCKeditor.ToolbarSet = 'Comment';
         oFCKeditor.Config["CustomConfigurationsPath"] = oFCKeditor.BasePath + "/hvn/config.js";
         
         div.innerHTML = oFCKeditor.CreateHtml();
         updateForm();
    }
	
	function updateForm()
	{
		//new reply
		$("#new-edit-form").ajaxForm({
			dataType:  'json',
			success: function(json) {
				if(json.success == '1'){
					alert('post success');
					$("#comment-text-" + globalId).html(json.comment_text); 
					$("#comment-row-" + globalId).show();
					$("a.redraw").trigger("click");
					$("#box-wrapper").appendTo("#edit-table").hide();
					return false;
				}
				else{
					//alert when error found
					alert('can\'t insert');
					$("a.redraw").trigger("click");
					return false; 
				}
			}
		});
	}
	
    $(function (){
    	//change color in comment row
    	$("table#show-table tr:nth-child(odd)").addClass("striped");
    
    	//effect in search.phtml  
    	if($("#ratingOperator").val() != 'between')
    		$("#check").hide();
        $("#ratingOperator").change(function(e){
        	if($("#ratingOperator").val() == 'between')
				$("#check").show();
			else	
				$("#check").hide();
        });
       	
       	$("#form_id").submit(function(){
       		if ($("#rating").val() > $("#to_rating").val() && $("#ratingOperator").val() == 'between'){
       			alert("wrong rating!");
       			return false;
       		}
       		else
       			return true;
       	});
    
    //effect in list.phtml  
		$("#bulk").change(function(e){
			if($("#bulk").val() == 'status')
				$("#changeStatus").show();
			else
				$("#changeStatus").hide();
		});
		
		//bulk action
		$("#bulk-form").ajaxForm({
			dataType:  'json',
			success: function(json){
				
				if(json.bulkaction == 'delete')
				{	
					$.each(json.ids, function(i,id){
			            $("#comment-row-" + id).css('background-color','red').fadeOut(2000).remove();
			            console.log("Removing comment-box-" + id);	
      				});
      			}
      			else if(json.success == '1'){
      			 	if(json.bulkaction == 'status')
          			{
          				if(json.status == 'deleted' || json.status == 'spam'){
          					$.each(json.ids, function(i,id){
    				            $("#comment-row-" + id).css('background-color','blue').fadeOut(2000);
    				            $(".commentIdClass:checkbox:checked").click();
				            });
				        }
				        else
				        {
				        	$.each(json.ids, function(i,id){
				            	$(".commentIdClass:checkbox:checked").click();
				            	$("#comment-status-" + id).html(json.status);
				           	});
				        }
        			}
        			else
        				alert('fall');
    			}
    			//set list null
    			json.ids = null;
    		}
		});
		
		//delete href
		$("[id^=delete-comment-]").click(function(e){
			var del=confirm("Do you want to delete this comment?");
    		deleteId = this.id.split('-')[2];
    		if (del){	
    			$.getJSON("/en/comment/ajax/delete-comment", {'id': deleteId},
                function(json) {
                    if(json.success){	
                    	$("#comment-row-" + deleteId).css('background-color','red').fadeOut(2000);
                    	$("#box-wrapper").appendTo("#edit-table").hide();
     			    }                  
                    else
                        $("#delete-comment-" + deleteId).show().html("Could not delete");
                    return false;
    			});
    		}else
    			return false;
		});
		
		//spam href
		$("[id^=spam-comment-]").click(function(e){
			var spam=confirm("Do you want to spam this comment?");
    		spamId = this.id.split('-')[2];
           	if (spam){	
            	$.getJSON("/en/comment/ajax/edit-comment", {'status' : 'spam','id' : spamId},
                    function(json) {
                        if(json.success)	
                     	{  
                        	$("#box-wrapper").appendTo("#edit-table").hide();
                        	$("#comment-row-" + spamId).css('background-color','red').fadeOut(2000);
                        }
                        else
                    	{
                    		$("#spam-comment-" + spamId).show().html("Could not delete");
                        }
                });
            }
            else
            	return false;
		});
		
		// cancel button
		$("[id^=cancel]").click(function(e){
			$("#comment-row-" + globalId).show();	
			$("#box-wrapper").hide();
			$("#title").attr("value",'');
		});
		
		// quick edit href
		$("[id^=quick-edit-comment-]").click(function(e){
			$("#comment-row-" + globalId).show();	
			editId = this.id.split('-')[3];
			$("#title").attr("value",$("#comment-title-" + editId).text());
            $("#box-wrapper").show().insertAfter("#comment-row-" + editId);
            $("#check-captcha").hide();
            $("#comment-row-" + editId).hide(); 
            globalId = editId;
            mode = 1;
            if(typeof(oFCKeditor) != 'object')
                create_Fck();
            $("#new-edit-form").attr("action", '/en/comment/ajax/edit-comment?id='+globalId);
            updateForm();
        });
         
		// reply href
		$("[id^=reply-comment-]").click(function(e)
		{
			$("#comment-row-" + globalId).show();
        	replyId  = this.id.split('-')[2];
        	globalId   = replyId;
         	mode = 0 ;
			var parentId   = $("#parent-content-id-" + replyId).text();
			var parentType   = $("#parent-content-type-" + replyId).text();
            //create FCK
            $("#check-captcha").show();
            if (typeof(oFCKeditor) != 'object')
            	create_Fck();
           	$("#title").attr("value",'');
        	$("#box-wrapper").show().insertAfter("#comment-row-" + replyId);
        	$("#new-edit-form").attr("action", '/en/comment/ajax/new-comment?parent_content_id=' + parentId +'&parent_content_type=' + parentType + '&pid=' + replyId);
		});	//end reply
		

		$("[id^=comment-text-]").dblclick(function(e)
		{
			$("#comment-row-" + globalId).show();	
			$("#title").attr("value",'');
			editId = this.id.split('-')[2];
            $("#box-wrapper").show().insertAfter("#comment-row-" + editId);
            $("#check-captcha").hide();
            $("#comment-row-" + editId).hide(); 
            globalId = editId;
            mode = 1;
            if(typeof(oFCKeditor) != 'object')
                create_Fck();
            $("#new-edit-form").attr("action", '/en/comment/ajax/edit-comment?id='+globalId);
            updateForm();
		});
		
    		
		$("#searchTime").change(function(e){
			if($("#searchTime").val() != 'detail')
    			$("#checkTime").hide();
    		else
    			$("#checkTime").show();
		});

		$("[id^=comment-row-]").mouseover(function(e){
			jQuery(this).find(".action-comment").show();
		}).mouseout(function(){
			jQuery(this).find(".action-comment").hide();
		});
   	});
